/**
 * 判断是否已登录
 *
 */
#include "../myfastdo/myfastdo.hpp"
#include "../utils.hpp"
#include "../protocol_dist.hpp"
USING_ALL;
FASTDO_API void ActionMain(MyFastdoServer &SERV)
{
    ProtocolC2SEditManga msg;
    msg.fromMixed(SERV.data.postMixed());

    ProtocolS2CEditManga rtn;

    Mixed field = $c{};
    // new
    if (0 == msg.manga->id)
    {
    }
    // edit
    else
    {
        field["id"] = msg.manga->id;
    }

    field["author"] = msg.manga->author;
    field["title"] = msg.manga->title;
    field["face"] = msg.manga->face;
    Mixed pageData = $a{};
    for (size_t i = 0; i < msg.manga->pages.size(); i++)
    {
        //小心，这里必须引用。不然msg.manga->pages里面的智能指针就失去数据了。
        auto& page = msg.manga->pages[i];
        pageData.add(page->toMixed());
    }
    field["pages"] = pageData.json();
    if (!SERV.getDBH().addNewRow("mangas", field))
    {
        throw RequestError("DB_WRITE_ERROR");
    }

    if (0 == msg.manga->id)
    {
        msg.manga->id = (int)SERV.getDBH().lastInsertId();
    }

    // rtn.manga->fromMixed(msg.manga->toMixed());
    rtn.manga = msg.manga;
    SERV.data.res.data = rtn.toMixed();
}